package com.googlecode.springgameengine.sample;

import com.googlecode.springgameengine.Game;
import com.googlecode.springgameengine.GameEngine;
import com.googlecode.springgameengine.Scene;
import com.googlecode.springgameengine.rendering.Renderer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Maciej
 */
public class SampleGameEngine implements GameEngine { //TODO: create a reusable GameEngine class from this sample

	private static final Log log = LogFactory.getLog(SampleGame.class);
	private long lastFrameTime;
	private Game game;
	private Renderer renderer;

	@Override
	public void run() {
		log.info("Running game "+game.getClass().getSimpleName());
		
		renderer.init();

		lastFrameTime = getTime();

		while (!game.isFinished()) {

			Scene scene = game.update(getDelta());

			renderer.render(scene);
			//TODO: process input and audio
		}

		renderer.destroy();
		
		log.info("Game finished!");
	}

	private long getDelta() {
		long time = getTime();
		long delta = time - lastFrameTime;
		lastFrameTime = time;

		return delta;
	}

	public long getTime() {
		return System.nanoTime() / 1000000;
	}

	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}

	public Renderer getRenderer() {
		return renderer;
	}

	public void setRenderer(Renderer renderer) {
		this.renderer = renderer;
	}
}
